package com.example.mypro;

import com.example.domain.TuShu;
import com.example.factory.ServiceFactory;
import com.example.service.TuShuService;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SearchBookActivity extends Activity implements OnClickListener{
	private TuShuService tuShuService;
	private Button search;
	private EditText bookname;
	private TextView Jy_sj;
	private TextView Ts_cbs;
	private TextView Ts_jj;
	private TextView Ts_sm;
	private TextView Ts_zz;
	private TuShu tuShu=new TuShu();
	private GetData getData;
	
	protected void onCreate(Bundle savedInstanceState) {
		tuShuService=ServiceFactory.getTuShuService();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_searchbook);
		search=(Button)findViewById(R.id.searchbook);
		bookname=(EditText)findViewById(R.id.bookname);
		Jy_sj = (TextView) findViewById(R.id.search_tv_sj);
		Ts_jj = (TextView) findViewById(R.id.search_tv_jj);
		Ts_sm = (TextView) findViewById(R.id.search_tv_sm);
		Ts_cbs = (TextView) findViewById(R.id.search_tv_cbs);
		Ts_zz = (TextView) findViewById(R.id.search_tv_zz);
		search.setOnClickListener(this);
		}
	@SuppressLint("ShowToast")
	class GetData extends AsyncTask<Void, Void, TuShu> {

		@SuppressLint("ShowToast")
		@Override
		protected TuShu doInBackground(Void... params) {
			System.out.println("doInBackground");
			tuShu=tuShuService.geTuShu(bookname.getText().toString().trim());
			return tuShu;
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);

		}

		@Override
		protected void onPostExecute(TuShu result) {
			super.onPostExecute(result);
			if (tuShu != null) {
		    Ts_jj.setText(tuShu.getTs_jj());
		    Jy_sj.setText(tuShu.getJy_sj());
			Ts_sm.setText(tuShu.getTs_sm());
			Ts_cbs.setText(tuShu.getTs_cbs());
			Ts_zz.setText(tuShu.getTs_zz());
			}
		}

	}

	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.searchbook)
		{  
            getData = new GetData();
			getData.execute();
		if("null".equals(tuShu.getTs_sm()))
		{
			Toast.makeText(this, "没有搜索结果",Toast.LENGTH_LONG).show();
			
		}
  }
		
	}
	}